package cn.biplam.front.component.data.tree.treeIndex;

import cn.biplam.common.ajxcode.MdCode.DwObjectSelectCode;
import cn.biplam.common.ajxcode.MdCode.DwTableImportFromDbCode;
import cn.biplam.common.ajxcode.MdCode.DwTableImportFromFileCode;
import cn.biplam.common.ajxcode.MdCode.DwTableMaintainCode;
import cn.biplam.common.ajxcode.MdCode.DwThemeMaintainCode;
import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.DbResource;
import cn.biplam.common.opcode.mdManage.TypeCode.OptModelIndexCode;
import cn.biplam.common.paramters.MdParam;
import cn.biplam.common.paramters.ModelType.Model_Type;
import cn.biplam.common.struc.BaseStruc;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.UserInfo;
import cn.biplam.front.component.data.common.CaptionComp;
import cn.biplam.front.component.data.tree.TreeChainData;
import cn.biplam.front.component.data.tree.TreeChainDataMap;
import cn.biplam.front.component.data.tree.TreeData;
import cn.biplam.front.component.data.tree.TreeIndexData;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.htmlscript.script.AjxScript.Level;
import cn.biplam.front.template.callback.TreeCallBack;
import cn.biplam.front.template.callback.tree.DwTreeCallBack;
import cn.biplam.front.template.control.Tree;
import cn.biplam.front.template.control.tree.RightMenu;

/**
 * 仓库对象模型DC
 * 	返回回调函数，这个函数提供 获取TreeData数据所需的operatorId
 * 	提供TreeData给VC
 * 	对象的维护(增删改)
 * @author WangSong 2009-6-8下午04:42:55
 * @version 1.0
 */
public class DwTree extends ModelTreeDc {

	public static String tableName = "model_index";
	/**仓库对象模型类型，这个类型和OperatorType分开，在取根节点时用过过滤条件 */
	private String nodeType = Model_Type.Dw.toString();
	
	/**与树的标准字段的映射 */
	private TreeChainDataMap fieldMap;
//	/** 获取主题树的持久化ID */
//	private String operatorID = OptModelIndexCode.Get_Model_Theme_Tree;
	
	private boolean virtualNodeVisiable=false;

	/**
	 * 右键的操作类型定义
	 */
	public enum Dw_Operator_Type{

		/**根主题维护，此主题只能管理员维护 */
		RootSubject("1"),
		/**非根主题维护,此主题可以在根主题上新建，在非根主题上修改删除 */
		UnRootSubject("2"),
		/**业务对象维护 */
		Table("3");

		private String value;
		private Dw_Operator_Type(String value){this.value=value;}
		public String toString() {return value;}
	}
	
	/**
	 * 构造方法
	 * @param mUser    用户信息
	 * @param sHomeUrl 系统路径
	 */
	public DwTree(UserInfo mUser,String sHomeUrl){
		this.mUser = mUser;
		this.sHomeUrl = sHomeUrl;
	}

	/**
	 *  设置虚根节点是否显示
	 * @param virtualNodeVisialble
	 */
	public void setVirtualNodeVisiable(boolean virtualNodeVisialble){
		this.virtualNodeVisiable = virtualNodeVisialble;
	}
	
	/**
	 * 返回回调函数，这个函数提供 获取TreeData数据所需的operatorId
	 */
	public TreeCallBack getCallBack(String targetID,String actionId,String businessFlag){
		if(businessFlag==null){
			return new DwTreeCallBack(targetID,actionId);
		}else{
			return new DwTreeCallBack(targetID,actionId,businessFlag);
		}
	}

	/**
	 * 设置取指定节点的子树参数信息
	 * @param userID      用户ID
	 * @param nodeId      当前节点id
	 * @param nodeLevel   当前节点级别，当取根节点的时候 级别为0
	 * @param levelDown   向下取几级数据 -1 全部 0 当前  1 当前节点下1级 2当前节点向下2级 ... n当前节点向下n级
	 * @param parentsHaveLBrother  父亲们是否有弟弟
	 * @throws ErrorMessage
	 */
	public TreeData getIndexTreeData(String userID,
			String nodeId,
			String nodeLevel,
			String levelDown,
			String haveBrother,
			String haveLBrother,
			String parentsHaveLBrother) throws ErrorMessage{
		return this.getTreeData(userID, nodeId, nodeLevel, levelDown,
				haveBrother,
				haveLBrother,
				parentsHaveLBrother, new DbResource());
	}

	/**
	 * 设置取指定节点的子树参数信息
	 * @param userID      用户ID
	 * @param nodeId      当前节点id
	 * @param nodeLevel   当前节点级别
	 * @param levelDown   向下取几级数据 -1 全部 0 当前  1 当前节点下1级 2当前节点向下2级 ... n当前节点向下n级
	 * @param parentsHaveLBrother  父亲们是否有弟弟
	 * @param viewTheme   只显示主题
	 * @param dbr      数据操作数据源
	 * @throws ErrorMessage
	 */
	public TreeData getTreeData(String userID,
			String nodeId,
			String nodeLevel,
			String levelDown,
			String haveBrother,
			String haveLBrother,
			String parentsHaveLBrother,
			DbResource dbr) throws ErrorMessage{
		TreeIndexData tid = new TreeIndexData();
		tid.setData(tableName, userID, nodeId,nodeLevel, levelDown,nodeType,
				haveBrother,
				haveLBrother,parentsHaveLBrother,dbr);
		//设置虚根
		tid.setVirtualRootNode("数据仓库模型", null);
		tid.setVirtualNodeVisiable(this.virtualNodeVisiable);
		tid.setLevelDown(TOOLS.stringToInt(levelDown));
		tid.setFilterOperator(this.filterOperatorId);
		tid.create();
		return tid;
	}

	/**
	 * 设置取指定节点的子树参数信息
	 * @return
	 * @throws ErrorMessage 
	 */
	public TreeData getTreeData(String userID,
			String nodeId,
			String levelDown,
			String nodeLevel,
			String haveBrother,
			String haveLBrother,
			String parentsHaveLBrother) throws ErrorMessage{
		return this.getTreeData(userID, nodeId, levelDown, nodeLevel, haveBrother, 
				haveLBrother, parentsHaveLBrother, 
				null, OptModelIndexCode.Get_Model_Theme_Tree);
	}

	/**
	 * 设置取指定节点的子树参数信息
	 * @return
	 * @throws ErrorMessage 
	 */
	public TreeData getTreeData(String userID,
			String nodeId,
			String levelDown,
			String nodeLevel,
			String haveBrother,
			String haveLBrother,
			String parentsHaveLBrother,
			String operatorID) throws ErrorMessage{
		return this.getTreeData(userID, nodeId, levelDown, nodeLevel, haveBrother, 
				haveLBrother, parentsHaveLBrother, 
				null, operatorID);
	}
	
	/**
	 * 设置取指定节点的子树参数信息
	 * @return
	 * @throws ErrorMessage 
	 */
	public TreeData getTreeData(String userID,
			String nodeId,
			String levelDown,
			String nodeLevel,
			String haveBrother,
			String haveLBrother,
			String parentsHaveLBrother,
			DbResource dbResource,
			String operatorID) throws ErrorMessage{
		this.setFieldMap();
		DataFilter filter = new DataFilter();
		filter.put(BaseStruc.IndexTableStruc.NODE_TYPE, Model_Type.Dw.toString());
		filter.put("level_down", levelDown);
		filter.put(BaseStruc.IndexTableStruc.NODE_ID, nodeId);
		TreeChainData tcd = new TreeChainData(operatorID,
				userID,
				filter,
				this.fieldMap,
				TOOLS.stringToInt(levelDown),
				dbResource);
		tcd.setData(nodeLevel, haveBrother, haveLBrother, parentsHaveLBrother);
		tcd.setVirtualRootNode("数据仓库模型", null);
		tcd.setVirtualNodeVisiable(this.virtualNodeVisiable);
		tcd.create();
		return tcd;
	}	

	/**
	 * 增删改操作
	 */
	@Override
	protected HtmlWindow ajxGet() throws ErrorMessage {
		return null;
	}

	/**
	 * 右键菜单页面
	 * @return
	 * @throws ErrorMessage 
	 */
	public void setRightMenu(Tree mTree) throws ErrorMessage{
		String rmName = "dwRm1";
		/**根主题，此主题只能管理员维护 */
		RightMenu dwRm1=new RightMenu(rmName,this.actionId);
		//新建主题
		dwRm1.set("1",CaptionComp.get(mUser,"223"),"treeS('"+DwThemeMaintainCode.Theme_Add+"','"+rmName+"')");
		//导入模型
		dwRm1.set("2",CaptionComp.get(mUser,"225"),"treep('"+DwTableImportFromFileCode.Table_Import_Act+"','"+Level.First.toString()+"')");
		//扫描数据库
		dwRm1.set("3",CaptionComp.get(mUser,MdParam.Scan_Database),"treep('"+DwTableImportFromDbCode.Get_Import_Panel+"','"+Level.First.toString()+"')");
//		//修改
//		dwRm1.set("4",CaptionComp.get(mUser,SysCaption.Edit),"treeS('"+DwThemeMaintain.Root_Theme_Edit+"','"+rmName+"')");

		/**非根主题,此主题可以在根主题上新建，在非根主题上修改删除 */
		rmName = "dwRm2";
		RightMenu dwRm2=new RightMenu(rmName,this.actionId);
		dwRm2.set("1",CaptionComp.get(mUser,"223"),"treeS('"+DwThemeMaintainCode.Theme_Add+"','"+rmName+"')");
		//新建表
		dwRm2.set("2",CaptionComp.get(mUser,"224"),"treeS('"+DwTableMaintainCode.Object_Add+"','"+rmName+"')");
		//修改
		dwRm2.set("3",CaptionComp.get(mUser,"27"),"treeS('"+DwThemeMaintainCode.Sub_Theme_Edit+"','"+rmName+"')");
		//删除
		dwRm2.set("4",CaptionComp.get(mUser,"2"),"treeS('"+DwThemeMaintainCode.Theme_Del+"','"+rmName+"')");
		//移动到
		dwRm2.set("5",CaptionComp.get(mUser,"82"),"treeS('"+DwObjectSelectCode.Get_Dw_Move_Tree+"','"+rmName+"')");

		/**表维护 */
		rmName = "dwRm3";
		RightMenu dwRm3=new RightMenu(rmName,this.actionId);
		//修改
		dwRm3.set("1",CaptionComp.get(mUser,"27"),"treeS('"+DwTableMaintainCode.Object_Edit+"','"+rmName+"')");
		//删除
		dwRm3.set("2",CaptionComp.get(mUser,"2"),"treeS('"+DwTableMaintainCode.Object_Del+"','"+rmName+"')");
		//复制到
		dwRm3.set("3",CaptionComp.get(mUser,"81"),"treeS('"+DwTableMaintainCode.Object_Edit+"','"+rmName+"')");
		//移动到
		dwRm3.set("4",CaptionComp.get(mUser,"82"),"treeS('"+DwObjectSelectCode.Get_Dw_Move_Tree+"','"+rmName+"')");

		/**右键菜单设置 根据索引表的Operator_Type进行设置 */
		mTree._setRightMenu(dwRm1.get(),  nodeType+Dw_Operator_Type.RootSubject.toString());
		mTree._setRightMenu(dwRm2.get(),  nodeType+Dw_Operator_Type.UnRootSubject.toString());
		mTree._setRightMenu(dwRm3.get(),  nodeType+Dw_Operator_Type.Table.toString());
	}

	/**
	 * 设置字段映射
	 */
	private void setFieldMap(){
		this.fieldMap = TreeChainDataMap.getFieldMap();
	}
}
